package com.zmj.sy.mom.srv.aps.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zmj.sy.mom.srv.aps.bean.dto.wms.WmsPartOutboundReqDto;
import com.zmj.sy.mom.srv.aps.bean.dto.wms.WmsWLReqDto;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.powder.PowderOutNoticeApsReqVo;
import com.zmj.sy.mom.srv.aps.service.WmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Api(tags = "ERP")
@RestController
@RequestMapping("/wms")
public class WmsController {

    private final WmsService wmsService;
    private final ObjectMapper objectMapper;

    @ApiOperation("外购件采购单")
    @PostMapping("/partProcurement")
    public BaseResVo partProcurement(WmsWLReqDto reqVo) {
        wmsService.partProcurement(reqVo.getId(), reqVo.getCoverFlag());
        return BaseResVo.ok();
    }

    @ApiOperation("外购件入库——新")
    @PostMapping(value = "/wlPartOutNew", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    public String wlPartOutNew(@RequestBody String strXml) {
        log.info("立体库外购件领料出库,{}",strXml);
        return wmsService.wlPartOutNew(strXml);
    }

    @ApiOperation("外购件入库")
    @PostMapping("/wlPartOut")
    public BaseResVo partOutbound(@RequestBody String body) {

        log.info("外购件入库-原始结果：{}", body);
        Map<String, String> resVo = new HashMap<>();

        try {

            List<WmsPartOutboundReqDto> reqVo = objectMapper.readValue(body, new TypeReference<List<WmsPartOutboundReqDto>>() {});
            log.info("外购件入库-转换后的结果：{}", objectMapper.writeValueAsString(reqVo));

            wmsService.partOutbound(reqVo);

            resVo.put("RESULT", "S");
            resVo.put("MESSAGE", "成功");

        } catch ( Exception e){
            e.printStackTrace();
            resVo.put("RESULT", "E");
            resVo.put("MESSAGE", e.getMessage());

            return BaseResVo.fail(e.getMessage());
        }
        return BaseResVo.ok();
    }

    @ApiOperation("粉末定额出库通知备件库")
    @PostMapping("/powderOutReceiveAps")
    public BaseResVo powderOutReceiveAps(@RequestBody BaseIdReqVo reqVo) {
        wmsService.powderOutReceiveAps(reqVo.getId());
        return BaseResVo.ok();
    }

    @ApiOperation("粉末领料单撤回")
    @PostMapping("/powderOutDelete")
    public BaseResVo powderOutDelete(@RequestBody BaseIdReqVo reqVo) {
        wmsService.powderOutDelete(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("粉末领料出库通知APS")
    @PostMapping("/powderOutNoticeAps")
    public BaseResVo powderOutNoticeAps(@RequestBody List<PowderOutNoticeApsReqVo> reqVo) {
        wmsService.powderOutNoticeAps(reqVo);
        return BaseResVo.ok();
    }

}
